package com.bawei.module_home.goods_act;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;


import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bawei.library_base.RouterPath;
import com.bawei.library_base.mvp.v.BaseActivity;

import com.bawei.module_home.R;
import com.bawei.module_home.mvp.Goods;
import com.bawei.module_home.mvp.GoodsContract;
import com.bawei.module_home.mvp.GoodsModel;
import com.bawei.module_home.mvp.GoodsPresenter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.List;

import scut.carson_ho.searchview.ICallBack;
import scut.carson_ho.searchview.SearchView;
import scut.carson_ho.searchview.bCallBack;


@Route(path = RouterPath.SearchGoods.SEARCH_GOODS)
public class GoodsActivity extends BaseActivity<GoodsPresenter> implements GoodsContract.GoodsView {


    private SearchView actGoodsSearch;
    private RecyclerView actGoodsRv;
    private SearchGoodsAdapter searchGoodsAdapter;

    @Override
    public int beanLayout() {
        return R.layout.activity_goods;
    }

    @Override
    public void initView() {
        actGoodsSearch = (SearchView) findViewById(R.id.act_goods_search);

        actGoodsRv = (RecyclerView) findViewById(R.id.act_goods_rv);
        actGoodsRv.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        actGoodsRv.setOverScrollMode(View.OVER_SCROLL_NEVER);
        mPresenter = new GoodsPresenter(new GoodsModel(), GoodsActivity.this);
    }

    @Override
    public void initData() {

        //搜索内容
        search();
        //设置点击返回建操作
        back();

    }
    //2页面跳转来
    @Autowired
    public String name;
    @Autowired
    public boolean ok;

    private void search() {
        ARouter.getInstance().inject(this);

        Log.i("3333333TAG", "search: "+name+ok);
       if (ok){
           mPresenter.getGoods(""+name+"","",1,20);

       }


        actGoodsSearch.setOnClickSearch(new ICallBack() {
            @Override
            public void SearchAciton(String string) {

                mPresenter.getGoods(""+string+"","",1,20);
                if (searchGoodsAdapter!=null){
                    searchGoodsAdapter.getData().clear();
                    searchGoodsAdapter.notifyDataSetChanged();
                }

            }
        });
    }

    private void back() {
        actGoodsSearch.setOnClickBack(new bCallBack() {
            @Override
            public void BackAciton() {
                finish();
            }
        });
    }

    @Override
    public void initAdapter(List<Goods.DataBean> dataBeans) {
        searchGoodsAdapter = new SearchGoodsAdapter(dataBeans);
        actGoodsRv.setAdapter(searchGoodsAdapter);

        searchGoodsAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                    ARouter.getInstance().build(RouterPath.DetailsGoods.DETAILS_GOODS)
                            .withSerializable("dataBeans",dataBeans.get(position))
                            .navigation();
            }
        });
//        if (dataBeans!=null){
//            searchGoodsAdapter.getData().clear();
//        }else {
//
//           searchGoodsAdapter.getData().addAll(dataBeans);
//        }


    }
}